package com.smsc.headend.connector.controller;

import com.smsc.headend.common.utils.R;
import com.smsc.headend.connector.manager.AssociationManager;
import com.smsc.headend.connector.netty.NettyConfigHelper;
import com.smsc.headend.connector.ping.PingUtil;
import com.smsc.headend.connector.service.DeviceConnectService;
import com.smsc.headend.connector.service.FeignAssetManagementService;
import com.smsc.headend.connector.service.TerminalChannelService;
import com.smsc.headend.connector.snmp.service.SnmpService;
import com.smsc.headend.module.asset.entity.ComEntity;
import com.smsc.headend.module.ping.PingParams;
import com.smsc.headend.module.ping.PingResponseDto;
import com.smsc.headend.module.protocol.cosem.util.ByteUtils;
import com.smsc.headend.module.task.dto.ConnectorTask;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.UUID;

@RestController
@RequestMapping("connector")
@Slf4j
public class ConnectorController {

    @Autowired
    DeviceConnectService deviceConnectService;
    @Autowired
    FeignAssetManagementService assetManagementService;
    @Autowired
    TerminalChannelService terminalChannelService;
    @Autowired
    AssociationManager associationManager;
    @Autowired
    SnmpService snmpService;

    @RequestMapping("test")
    public R testConnectAAWithPacket(@RequestParam Long terminalId
            , @RequestParam(value = "assetNo", required = false) Long meterId
            , @RequestParam String packetString) {

        ConnectorTask task = new ConnectorTask();
        task.setAtomicTaskNo(UUID.randomUUID().toString());
        task.setTaskNo(UUID.randomUUID().toString());
        task.setMeterId(meterId);
        task.setTerminalId(terminalId);
        task.setPacketBytes(new byte[][]{ByteUtils.hexStringToByte(packetString)});
        task.setRoleId(1);
        task.setReturnPacket(false);
        deviceConnectService.sendToDevice(task);
        return R.ok();
    }

    @RequestMapping("sendDirect")
    public R sendByteToDeviceDirectly(@RequestParam Long terminalId
            , @RequestParam String packetString) {
        ComEntity comEntity = assetManagementService.getComById(terminalId);
        deviceConnectService.sendPacketDirect(comEntity, ByteUtils.hexStringToByte(packetString));
        return R.ok();
    }

    @RequestMapping("testEvent")
    public R testLog(@RequestParam Long terminalId) {
        Channel channel = NettyConfigHelper.getChannelByComId(terminalId);
        byte[] eventByte = ByteUtils.hexStringToByte("00 01 00 01 00 01 00 52 0F 80 E8 A8 25 0C 07 E4 08 11 01 0E 37 15 FF 80 00 00 02 0B 09 0C 07 E4 " +
                "08 11 01 0E 33 0A FF 80 00 00 12 00 01 06 00 00 00 00 06 00 00 00 00 06 00 00 00 00 06 00 00 00 " +
                "00 06 00 00 00 00 06 00 00 00 00 05 00 00 00 00 05 00 00 00 00 05 00 00 00 00");
        deviceConnectService.dlmsPacketAccept(channel, eventByte);
        return R.ok();
    }

    @RequestMapping("kmsMock")
    public R kmsMock(@RequestParam("BizContent") String bizContent) {
        log.info("get mock request bizContent={}", bizContent);
        return R.data(new HashMap<String, String>() {{
            put("AK", "30303030303030303030303030303030");
            put("EK", "30303030303030303030303030303030");
        }});
    }

    @RequestMapping("closeChannel")
    public R closeChannel(@RequestParam String moduleSn) {
        terminalChannelService.closeAnonyChannel(moduleSn);
        return R.ok();
    }

    @RequestMapping("pingMeter")
    public R pingMeter(@RequestBody PingParams pingParams) {
        PingResponseDto pingResponseDto = PingUtil.pingMeter(pingParams);
        return R.data(pingResponseDto);
    }

    @RequestMapping("getNetworkFeature")
    public R getNetworkFeature(@RequestBody Long dcuId) {
        return snmpService.getNetworkFeature(dcuId);
    }

}
